package com.huawei.hms.support.api.push.b.c;

import android.R.drawable;
import android.annotation.TargetApi;
import android.app.Notification.Builder;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Icon;
import android.os.Build;
import com.huawei.hms.android.HwBuildEx.VERSION;
import com.huawei.hms.support.log.HMSLog;

public class a {
    private static int a(Context context, String str, String str2, Object obj) {
        Throwable e;
        Throwable th;
        int i;
        try {
            String str3 = context.getPackageName() + ".R";
            HMSLog.d("PushSelfShowLog", "try to refrect " + str3 + " typeName is " + str2);
            Class[] classes = Class.forName(str3).getClasses();
            HMSLog.d("PushSelfShowLog", "sonClassArr length " + classes.length);
            for (Class cls : classes) {
                String substring = cls.getName().substring(str3.length() + 1);
                HMSLog.d("PushSelfShowLog", "sonTypeClass,query sonclass is " + substring + " sonClass.getName() is" + cls.getName());
                if (str2.equals(substring)) {
                    break;
                }
            }
            Class cls2 = null;
            if (cls2 != null) {
                i = cls2.getField(str).getInt(obj);
                try {
                    HMSLog.d("PushSelfShowLog", "refect res id is " + i);
                } catch (ClassNotFoundException e2) {
                    e = e2;
                    HMSLog.e("PushSelfShowLog", "ClassNotFound failed,", e);
                    return i;
                } catch (NoSuchFieldException e3) {
                    e = e3;
                    HMSLog.e("PushSelfShowLog", "NoSuchFieldException failed,", e);
                    return i;
                } catch (IllegalAccessException e4) {
                    e = e4;
                    HMSLog.e("PushSelfShowLog", "IllegalAccessException failed,", e);
                    return i;
                } catch (IllegalArgumentException e5) {
                    e = e5;
                    HMSLog.e("PushSelfShowLog", "IllegalArgumentException failed,", e);
                    return i;
                } catch (IndexOutOfBoundsException e6) {
                    e = e6;
                    HMSLog.e("PushSelfShowLog", "IndexOutOfBoundsException failed,", e);
                    return i;
                }
                return i;
            }
            HMSLog.d("PushSelfShowLog", "sonTypeClass is null");
            String str4 = context.getPackageName() + ".R$" + str2;
            HMSLog.d("PushSelfShowLog", "try to refrect 2 " + str4 + " typeName is " + str2);
            i = Class.forName(str4).getField(str).getInt(obj);
            HMSLog.d("PushSelfShowLog", " refect res id 2 is " + i);
            return i;
        } catch (Throwable e7) {
            th = e7;
            i = 0;
            e = th;
            HMSLog.e("PushSelfShowLog", "ClassNotFound failed,", e);
            return i;
        } catch (Throwable e72) {
            th = e72;
            i = 0;
            e = th;
            HMSLog.e("PushSelfShowLog", "NoSuchFieldException failed,", e);
            return i;
        } catch (Throwable e722) {
            th = e722;
            i = 0;
            e = th;
            HMSLog.e("PushSelfShowLog", "IllegalAccessException failed,", e);
            return i;
        } catch (Throwable e7222) {
            th = e7222;
            i = 0;
            e = th;
            HMSLog.e("PushSelfShowLog", "IllegalArgumentException failed,", e);
            return i;
        } catch (Throwable e72222) {
            th = e72222;
            i = 0;
            e = th;
            HMSLog.e("PushSelfShowLog", "IndexOutOfBoundsException failed,", e);
            return i;
        }
    }

    private static int b(Context context, com.huawei.hms.support.api.push.b.b.a aVar) {
        if (context == null || aVar == null) {
            HMSLog.i("PushSelfShowLog", "enter getSmallIconId, context or msg is null");
            return 0;
        }
        int i = context.getApplicationInfo().icon;
        if (i != 0) {
            return i;
        }
        i = context.getResources().getIdentifier("btn_star_big_on", "drawable", "android");
        HMSLog.d("PushSelfShowLog", "icon is btn_star_big_on ");
        if (i != 0) {
            return i;
        }
        HMSLog.d("PushSelfShowLog", "icon is sym_def_app_icon ");
        return 17301651;
    }

    public static Bitmap a(Context context, com.huawei.hms.support.api.push.b.b.a aVar) {
        Bitmap bitmap;
        Throwable th;
        Throwable th2;
        Bitmap bitmap2 = null;
        if (context == null || aVar == null) {
            return null;
        }
        try {
            if (aVar.l() != null && aVar.l().length() > 0) {
                int i = 0;
                if (!aVar.l().equals("" + aVar.a())) {
                    i = a(context, aVar.l(), "drawable", new drawable());
                    if (i == 0) {
                        i = context.getResources().getIdentifier(aVar.l(), "drawable", "android");
                    }
                    HMSLog.d("PushSelfShowLog", "msg.notifyIcon is " + aVar.l() + ",and defaultIcon is " + i);
                }
                if (i != 0) {
                    bitmap2 = BitmapFactory.decodeResource(context.getResources(), i);
                }
            }
            try {
                if (VERSION.EMUI_SDK_INT >= 11) {
                    HMSLog.i("PushSelfShowLog", "huawei phone, and emui5.0, need not show large icon.");
                    return bitmap2;
                }
                if (bitmap2 == null) {
                    if (!"com.huawei.android.pushagent".equals(aVar.i())) {
                        HMSLog.i("PushSelfShowLog", "get left bitmap from " + aVar.i());
                        bitmap = ((BitmapDrawable) context.getPackageManager().getApplicationIcon(aVar.i())).getBitmap();
                        return bitmap;
                    }
                }
                bitmap = bitmap2;
                return bitmap;
            } catch (Throwable e) {
                th = e;
                bitmap = bitmap2;
                th2 = th;
                HMSLog.e("PushSelfShowLog", "" + th2.toString(), th2);
                return bitmap;
            } catch (Throwable e2) {
                th = e2;
                bitmap = bitmap2;
                th2 = th;
                HMSLog.e("PushSelfShowLog", "" + th2.toString(), th2);
                return bitmap;
            }
        } catch (Throwable e22) {
            th = e22;
            bitmap = null;
            th2 = th;
            HMSLog.e("PushSelfShowLog", "" + th2.toString(), th2);
            return bitmap;
        } catch (Throwable e222) {
            th = e222;
            bitmap = null;
            th2 = th;
            HMSLog.e("PushSelfShowLog", "" + th2.toString(), th2);
            return bitmap;
        }
    }

    @TargetApi(23)
    private static Icon c(Context context, com.huawei.hms.support.api.push.b.b.a aVar) {
        Icon icon = null;
        if (context == null || aVar == null) {
            HMSLog.e("PushSelfShowLog", "getSmallIcon, context is null");
        } else if (Build.VERSION.SDK_INT < 23) {
            HMSLog.i("PushSelfShowLog", "getSmallIcon failed, Build.VERSION less than 23");
        } else {
            try {
                icon = Icon.createWithResource(aVar.i(), context.getPackageManager().getApplicationInfo(aVar.i(), 0).icon);
            } catch (NameNotFoundException e) {
                HMSLog.e("PushSelfShowLog", e.toString());
            } catch (Throwable e2) {
                HMSLog.e("PushSelfShowLog", e2.toString(), e2);
            }
        }
        return icon;
    }

    private static boolean d(Context context, com.huawei.hms.support.api.push.b.b.a aVar) {
        return !"com.huawei.android.pushagent".equals(aVar.i()) && Build.VERSION.SDK_INT >= 23 && (com.huawei.hms.support.api.push.b.d.a.a(context) || com.huawei.hms.support.api.push.b.d.a.b(context));
    }

    @TargetApi(23)
    public static void a(Context context, Builder builder, com.huawei.hms.support.api.push.b.b.a aVar) {
        if (context == null || builder == null || aVar == null) {
            HMSLog.e("PushSelfShowLog", "msg is null");
        } else if (d(context, aVar)) {
            HMSLog.i("PushSelfShowLog", "get small icon from " + aVar.i());
            Icon c = c(context, aVar);
            if (c != null) {
                builder.setSmallIcon(c);
            } else {
                builder.setSmallIcon(b(context, aVar));
            }
        } else {
            builder.setSmallIcon(b(context, aVar));
        }
    }
}
